package client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JOptionPane;

/**
 * 
 * Main client socket class that is based from web article syntx.com
 * http://syntx.co/languages-frameworks/a-client-server-application-using-socket-programming-in-java/
 * 
 * Writing through socket was based from the Oracle Knowledge base
 * http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
 * 
 * @author Simon-Pierre Therriault
 */

public class SocketClient {
	

	static private Scanner scanIn;
	static private boolean done;
	
	//Client socket instances
	static private String hostname;
	static private Socket socket;
	static private int port;
	
	
	public SocketClient(String hostname, int port){
		this.hostname = hostname;
		this.port = port;
	}
	
	public static boolean connect() throws InterruptedException, IOException{
		
		try {
			System.out.println("Attempting to connect to (" + hostname + ":" + port + ")");
			socket = new Socket(hostname, port);
			
			Thread.sleep(2000);
			
			System.out.println("Connection established ... ");
		
			Thread.sleep(1000);
			
		} catch (UnknownHostException e) {
			JOptionPane.showMessageDialog(null,"error unknown hostname ..  : " + hostname);
			System.out.println(e);
			e.printStackTrace();
			return false;
			
		} catch (IOException e){
			JOptionPane.showMessageDialog(null,"error while establishing the connection .. ");
			System.out.println(e);
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public void readResponse() throws IOException, InterruptedException{
		String input;
		
		
		DataInputStream stdin = new DataInputStream(socket.getInputStream());
		
		System.out.println("Response from server ---> ");
		System.out.println("");
		
		Thread.sleep(1000);

		while((input = stdin.readLine()) != null){
			System.out.println("\t" + input);
			Thread.sleep(500);
		}
		
	}
	
	public static String createIpAddressWithPort(String ipAddress, int port){
		
		return ipAddress + ":" + port;
	}
	
	public static void showContextMenu(int menuSelect) throws IOException, InterruptedException{
		
		String conf;
		String quit;
		String data;
		
		switch (menuSelect) {
			case 1: 
				System.out.println("Enter port number : ");		 
				port = Integer.parseInt(scanIn.nextLine());
			    break;
				
			case 2:
				System.out.println("Enter IP Address destination : ");
				hostname = scanIn.nextLine();
				break;
			
			case 3:
				System.out.println("Start socket server connection (Y/N) : ");
				conf = scanIn.nextLine();
				
				if (conf.equals("Y") == true){
					
					SocketClient sClient = new SocketClient("localhost",7777);
					
					System.out.println("Enter the information you want to deliver to the server : ");
					data = scanIn.nextLine();
					
					sClient.connect();
					sendDataToServer(data);
					sClient.readResponse();
				}	
				break;
			case 99:
				
				System.out.println("\n\n\nDo you want to quit (Y/N) : ");
				quit = scanIn.nextLine();
				
				done = (quit.equals("Y") == true) ? false : true;
				break;
				
			default:
				System.out.println("bad menu selection .. please revalidate");
				break;
		
		}
	}
	
	private static void sendDataToServer(Object data) throws IOException {
		
		PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
		out.println((String) data);
		out.flush();
		
	}

	public static void main(String[] args) throws IOException, InterruptedException {
		port = -1;
		done = true;
		scanIn = new Scanner(System.in);
		
		while(done){
			
			showContextMenu(3);
			
			Thread.sleep(2000);
			showContextMenu(99);
				
		}
		System.out.println("\nThank you of using my server .. Have a great day.");
		scanIn.close();
	}
}
